package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 标签Controller.
 *
 * @author 宫新程
 * @since 2019/3/2 10:08
 */
@CrossOrigin
@RestController
@RequestMapping("/label")
public class LabelController {

  @Resource private LabelService labelService;

  /** 查询全部. */
  @GetMapping
  public Result findAll(HttpServletRequest request) {
    String hello = request.getParameter("hello");
    System.out.println("Label Controller Get Hello：" + hello);

    String header = request.getHeader("Authorization");
    System.out.println("Authorization：" + header);

    List<Label> result = labelService.findAll();
    return new Result(true, StatusCode.OK, "查询成功", result);
  }

  /** 根据id查询. */
  @GetMapping("/{labelId}")
  public Result findById(@PathVariable String labelId) {
    Label result = labelService.findById(labelId);
    return new Result(true, StatusCode.OK, "查询成功", result);
  }

  /** 保存. */
  @PostMapping
  public Result save(@RequestBody Label label) {
    labelService.save(label);
    return new Result(true, StatusCode.OK, "添加成功");
  }

  /** 更新. */
  @PutMapping("/{labelId}")
  public Result update(@PathVariable String labelId, @RequestBody Label label) {
    label.setId(labelId);
    labelService.update(label);
    return new Result(true, StatusCode.OK, "修改成功");
  }

  /** 删除. */
  @DeleteMapping("/{labelId}")
  public Result delete(@PathVariable String labelId) {
    labelService.delete(labelId);
    return new Result(true, StatusCode.OK, "删除成功");
  }

  /** ====== 条件查询 ======. */
  @PostMapping("/search")
  public Result findSearch(@RequestBody Label label) {
    List<Label> result = labelService.findSearch(label);
    return new Result(true, StatusCode.OK, "查询成功", result);
  }

  /** ====== 分页条件查询 ======. */
  @PostMapping("/search/{page}/{size}")
  public Result pageQuery(
      @RequestBody Label label, @PathVariable int page, @PathVariable int size) {
    Page<Label> pageData = labelService.pageQuery(label, page, size);
    return new Result(
        true,
        StatusCode.OK,
        "查询成功",
        new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
  }
}
